package com.crawler.webhook;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.crawler.common.WebhookConstant;
import com.dingtalk.chatbot.DingtalkChatbotClient;
import com.dingtalk.chatbot.message.LinkMessage;
import com.dingtalk.chatbot.message.TextMessage;

/**
 * 报警器
 * @author rubekid
 *
 * 2017年9月30日 上午9:57:58
 */
public class Dtalk {

	public static final Logger LOGGER = LoggerFactory.getLogger(Dtalk.class);

	/**
	 * 发送消息
	 * @param user
	 * @param message
	 */
	public static void send(String message){
		if(WebhookConstant.DTALK_WEBHOOK != null){			
			send(message, WebhookConstant.DTALK_WEBHOOK);
		}
	}
	
	/**
	 * 发送消息
	 * @param user
	 * @param message
	 */
	public static void send(String content, String webhook){
		try{
			TextMessage message = new TextMessage(content);
			DingtalkChatbotClient client = new DingtalkChatbotClient();
	        client.send(webhook, message);
		}
		catch(Exception ex){
			LOGGER.error(ex.getMessage(), ex);
		}
	}
	
	/**
	 * 发送链接消息
	 * @param title 标题
	 * @param text 文本
	 * @param picUrl 图片地址
	 * @param messageUrl 链接
	 */
	public static void sentLinkMessage(String title, String text, String picUrl, String messageUrl){
		if(WebhookConstant.DTALK_WEBHOOK != null){			
			sendLinkMessage(title, text, picUrl, messageUrl, WebhookConstant.DTALK_WEBHOOK);
		}
	}
	
	/**
	 * 发送链接消息
	 * @param title 标题
	 * @param text 文本
	 * @param picUrl 图片地址
	 * @param messageUrl 链接
	 * @param webhook Webhook
	 */
	public static void sendLinkMessage(String title, String text, String picUrl, String messageUrl, String webhook){
		try{
			LinkMessage message = new LinkMessage();
			message.setTitle(title);
			message.setText(text);
			message.setPicUrl(picUrl);
			message.setMessageUrl(messageUrl);
			DingtalkChatbotClient client = new DingtalkChatbotClient();
	        client.send(webhook, message);
		}
		catch(Exception ex){
			LOGGER.error(ex.getMessage(), ex);
		}
	}
	
}